//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.dao.wills;

import com.aidong.dao.wills.BaseDao;
import com.aidong.dao.wills.PssProductStockDao;
import com.aidong.model.wills.PayList;
import com.aidong.model.wills.PssProductSale;
import com.aidong.model.wills.PssProductSaleList;
import com.aidong.model.wills.PssProductStock;
import com.aidong.model.wills.PssProductStockPK;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import org.springframework.stereotype.Repository;

@Repository
public class PssProductSaleDao extends BaseDao<PssProductSale, Integer> {
    public PssProductSaleDao() {
    }

    public void addSale(PssProductSale pssProductSale, List<PssProductSaleList> pssProductSaleList, PssProductStockDao pssProductStockDao, List<PayList> payList) {
        this.add(pssProductSale);
        Iterator i$ = pssProductSaleList.iterator();

        while(i$.hasNext()) {
            PssProductSaleList pay = (PssProductSaleList)i$.next();
            pay.setProductSaleId(pssProductSale.getAutoId());
            this.getSession().save(pay);
            this.updateStock(pssProductStockDao, pssProductSale.getShopCode(), pay.getProductCode(), pay.getQuantity(), pay.getCreateUser());
        }

        if(pssProductSale.getEarnestId() != null) {
            String i$1 = "update MemberEarnest set status=1, useSaleId=:p1, useDate=getdate() where saleId=:p2";
            this.updateBySql(i$1, new Object[]{pssProductSale.getAutoId(), pssProductSale.getEarnestId()});
        }

        i$ = payList.iterator();

        while(i$.hasNext()) {
            PayList pay1 = (PayList)i$.next();
            pay1.setSaleId(pssProductSale.getAutoId());
            this.getSession().save(pay1);
            if("S011".equalsIgnoreCase(pay1.getPayType())) {
                String sql = "update PayAli set status=:p1, payListId=:p2, salesCode=:p3, salesCode2=:p4, updateTime=:p5 where barCode=:p6";
                this.updateBySql(sql, new Object[]{Integer.valueOf(2), pay1.getAutoId(), pay1.getSalesCode(), pay1.getSalesCode2(), new Date(), pay1.getNote()});
            }
        }

    }

    private void updateStock(PssProductStockDao pssProductStockDao, String shopCode, String productCode, Double quantity, String createUser) {
        PssProductStockPK pssProductStockPK = new PssProductStockPK();
        pssProductStockPK.setProductCode(productCode);
        pssProductStockPK.setShopCode(shopCode);
        PssProductStock pssProductStock = (PssProductStock)pssProductStockDao.getById(pssProductStockPK);
        if(pssProductStock == null) {
            pssProductStock = new PssProductStock();
            pssProductStock.setProductCode(productCode);
            pssProductStock.setShopCode(shopCode);
            pssProductStock.setQuantity(Double.valueOf(-quantity.doubleValue()));
            pssProductStock.setCreateUser(createUser);
            pssProductStock.setCreateTime(new Date());
            pssProductStock.setUpdateTime(new Date());
            pssProductStockDao.add(pssProductStock);
        } else {
            pssProductStock.setQuantity(Double.valueOf(pssProductStock.getQuantity().doubleValue() - quantity.doubleValue()));
            pssProductStock.setUpdateTime(new Date());
            pssProductStockDao.update(pssProductStock);
        }

    }
}
